package com.groep14.project;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * De RegistratieController handelt requests af komende van de Registratie-pagina.
 * 
 * @since r33 - 3 April 2012
 * @author Maarten Tutak
 *
 */

@Controller
public class RegistratieController {

	private static final Logger logger = LoggerFactory.getLogger(RegistratieController.class);
	private MyLogger myLogger = new MyLogger();
	private StandardPasswordEncoder encoder;
	
	/**
	 * De registreer methode verzorgt de registratie van nieuwe klanten
	 * 
	 * @param request
	 * @param response
	 * @return ModalAndView
	 */
	
	@RequestMapping(value="/registreer",method=RequestMethod.POST)
	public ModelAndView registreer(HttpServletRequest request,HttpServletResponse response){
		
		ModelAndView m = new ModelAndView("redirect:login");
		
		if(request.getParameter("btnAction").equals("Bevestigen")){		
						
			logger.info("Registratie gestart.");
			
			try {
				
				Validator validator = new Validator();
				
				if (validator.validateString(request.getParameter("wachtwoord"), 0, 50, true, true, true) &&
					validator.validateEmail(request.getParameter("email")) &&
					validator.validateString(request.getParameter("familienaam"), 0, 50, false, true, false) &&
					validator.validateString(request.getParameter("voornaam"), 0, 50, false, true, false) &&
					validator.validateDate(request.getParameter("geboortedatum")) &&
					validator.validateCellphone(request.getParameter("telefoon")) &&
					validator.validateCellphone(request.getParameter("gsm")) &&
					validator.validateString(request.getParameter("adres"), 0, 50, true, true, false) &&
					validator.validateNumber(request.getParameter("postcode"), 1000, 9999, true)
					) {
				
					Connectie con = new Connectie();			
					con.setpStatement(con.getCon().prepareStatement("INSERT INTO persons VALUES (null,?,?,?,?,?,?,?,?,?,?,?,?)"));
					
					con.getpStatement().setString(1, request.getParameter("familienaam"));
					con.getpStatement().setString(2, request.getParameter("voornaam"));
					con.getpStatement().setObject(3, request.getParameter("geboortedatum"));
					con.getpStatement().setString(4, request.getParameter("geslacht"));
					con.getpStatement().setString(5, request.getParameter("telefoon"));
					con.getpStatement().setString(6, request.getParameter("gsm"));
					con.getpStatement().setString(7, request.getParameter("email"));
					con.getpStatement().setString(8, request.getParameter("land"));
					con.getpStatement().setString(9, request.getParameter("adres"));
					con.getpStatement().setString(10, request.getParameter("postcode"));
					
					//Password encryption..
					logger.info("Encrypting password..");
					encoder = new StandardPasswordEncoder(request.getParameter("email"));
					String encodedPassword = encoder.encode(request.getParameter("wachtwoord"));
					logger.info(encodedPassword);
					con.getpStatement().setString(11, encodedPassword);
					//
					
					con.getpStatement().setByte(12, new Byte("0"));
									
					if(con.getpStatement().executeUpdate() == 1){
						logger.info("Nieuwe klant tegevoegd aan database");
					} else {
						logger.info("Fout, Klant niet toegevoegd..");
						return new ModelAndView("redirect:registratie");
					}
				
				} else {
					logger.info("Not valid");
				}
				
			} catch (SQLException e1) {
				myLogger.logException(getClass() + " - ", e1);
			} catch (ClassNotFoundException e2) {
				myLogger.logException(getClass() + " - ", e2);
			} catch (InstantiationException e3) {
				myLogger.logException(getClass() + " - ", e3);
			} catch (IllegalAccessException e4) {
				myLogger.logException(getClass() + " - ", e4);
			} catch (Exception e) {
				myLogger.logException(getClass() + " - ", e);
			}
			
		} else {
			logger.info("Registratie geannuleerd.");
			return new ModelAndView("redirect:index");
		}
		
		return m;		
	}
}
